<script setup>
import {nextTick, onMounted, ref, watchEffect} from "vue";
import { effectiveProcessList } from '@/api/pharmacy/basis'
const tableData = ref([])
const multipleSelection = ref([])
const multipleTableRef = ref()
const props = defineProps({
  drugstoreRelation: { type: Array },
})
const updatePharmacyList = () => {
  console.log("pharmacy shou dao", props.drugstoreRelation)
  if (props.drugstoreRelation) {
    tableData.value = tableData.value.map(item=> {
      for (const drug of props.drugstoreRelation) {
        if (item.drugstore_id === drug.drugstore_id) {
          item.drug_process_ids = drug.drug_process_ids
          if (drug.is_recommend === 1) {
            item.is_recommend = true
          }
          setTimeout(() => {
            multipleTableRef.value.toggleRowSelection(item, true)
          }, 100)
        }
      }
      return item;
    })
  }
}
const getPharmacy = async () => {
  const res = await effectiveProcessList();
  tableData.value = res.data.map(item=> {
    item.is_recommend = false
    item.drug_process_ids = []
    return item
  })
}

const handleSelectionChange = (val) => {
  multipleSelection.value = val
}

onMounted(() => {
  getPharmacy()
})
defineExpose({
  multipleSelection,
  updatePharmacyList
})
</script>

<template>
<el-table ref="multipleTableRef" :data="tableData" @selection-change="handleSelectionChange">
  <el-table-column type="selection" width="55"> </el-table-column>
  <el-table-column prop="drugstore_id" label="药房ID" />
  <el-table-column prop="drugstore_name" label="药房名称" />
  <el-table-column prop="date" label="可用药态" width="500">
    <template #default="scope">
      <el-checkbox-group v-model="scope.row.drug_process_ids">
        <el-checkbox v-for="(p, pk) in scope.row.process_list" :key="pk" :value="p.id">{{ p.name }}</el-checkbox>
      </el-checkbox-group>
    </template>
  </el-table-column>
  <el-table-column prop="date" label="是否推荐药房">
    <template #default="scope">
      <el-radio v-model="scope.row.is_recommend"></el-radio>
    </template>
  </el-table-column>
</el-table>
</template>

<style scoped lang="scss">

</style>
